<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.artNo" placeholder="货号" clearable style="width: 130px;" class="filter-item" />
      <el-input v-model="listQuery.productName" placeholder="商品名称" clearable style="width: 130px; margin-left: 10px;" class="filter-item" />
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中">
      <el-table-column label="商品图片" align="center" min-width="110px">
        <template slot-scope="{row}">
          <div v-if="row.imgUrl">
            <viewer>
              <img :src="row.imgUrl" style="width: 150px; height: 60px; cursor:pointer">
            </viewer>
          </div>
          <div v-else>
            <span>无图片</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="货号" align="center" min-width="160px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.artNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" min-width="160px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.productName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80px">
        <template slot-scope="{row}">
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getMyFavoriteByPage, deleteMyFavoriteById } from '@/api/distributor/distribution-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'DistributorMyFavorite',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        artNo: null,
        productName: null
      },

      listLoading: true,
      list: null,
      total: 0

    }
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getMyFavoriteByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        artNo: null,
        productName: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    handleDelete(row) {
      deleteMyFavoriteById(row.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
